
clear 
set scheme lean2, permanent
global pm = char(177)
set more off

gl date = c(current_date)
if c(os) == "MacOSX" gl user "/Users/`c(username)'"
else if c(os) == "Windows" gl user "C:\Users\\`c(username)'"
else if c(os) == "Unix" gl user "/usr/`c(username)'"
di "$user
 
set graphics on  

global path_data_processed "\data\processed"
global path_output "\data\output"

 
clear all
set more off

cd "$path_data_processed"
use "experimentRNall.dta", clear

cd "$path_output"
 
gen ind_cyclical=1-health_educ-ind_other
tab ind_cyclical, m
gen ind_noncyclical=1-ind_cyclical

loc experiments "jobinsecurity jobloss jobfind"

foreach var of varlist republican democrat finlit_index{
gen mis_`var' = `var'==.
replace `var'=0 if `var'==.
}

replace log_income=0 if inc_NR==1
global controls age agesq female log_income inc_NR atleast_bachelor midwest south west republican democrat finlit_index mis_republican mis_finlit_index

gen posterior_ever_unemp = posterior*ever_unemployed_imp
gen tr_ever_unemp = treatment*ever_unemployed_imp
gen posterior_never_unemp = posterior*never_unemployed_imp
gen tr_never_unemp = treatment*never_unemployed_imp

gen posterior_highcountyunemp = posterior*highunemp_rate3y
gen tr_highcountyunemp = treatment*highunemp_rate3y
gen posterior_lowcountyunemp = posterior*(1-highunemp_rate3y)
gen tr_lowcountyunemp = treatment*(1-highunemp_rate3y)


gen posterior_unemp_rate2016= posterior*unemp_rate2016
gen tr_unemp_rate2016 = treatment*unemp_rate2016

gen posterior_highunemp_rate= posterior*highunemp_rate
gen tr_highunemp_rate = treatment*highunemp_rate




gen posterior_ind_cyclical = posterior*ind_cyclical
gen tr_ind_cyclical = treatment*ind_cyclical
gen posterior_ind_noncyclical = posterior*(1-ind_cyclical)
gen tr_ind_noncyclical = treatment*(1-ind_cyclical)




preserve

clear all
eststo clear
estimates drop _all

set obs 10
qui gen x = 1
qui gen y = 1

loc columns = 0

foreach choice in `experiments' {

    loc ++columns
    qui eststo col`columns': reg x y

}

restore


/* Statistics */

loc colnum = 1
loc colnames ""


foreach choice in `experiments' {
  
	ivreg2 `choice' (posterior posterior_ind_noncyclical = treatment tr_ind_noncyclical) ind_cyclical $controls, r
	sigstar posterior, prec(3)
    estadd loc thisstat4 = "`r(bstar)'": col`colnum'
    estadd loc thisstat5 = "`r(sestar)'": col`colnum'
	
    sigstar posterior_ind_noncyclical, prec(3)
    estadd loc thisstat7 = "`r(bstar)'": col`colnum'
    estadd loc thisstat8 = "`r(sestar)'": col`colnum'
		
	test 	posterior + posterior_ind_noncyclical = 0
    estadd loc thisstat10 = string(r(p), "%9.3f"): col`colnum'

	qui sum `choice' if ind_cyclical!=.
    estadd loc thisstat12 = r(N): col`colnum'
	
  
    ivreg2 `choice' (posterior posterior_never_unemp = treatment tr_never_unemp) never_unemployed_imp $controls, r
    sigstar posterior, prec(3)
    estadd loc thisstat17 = "`r(bstar)'": col`colnum'
    estadd loc thisstat18 = "`r(sestar)'": col`colnum'
	
    sigstar posterior_never_unemp, prec(3)
    estadd loc thisstat20 = "`r(bstar)'": col`colnum'
    estadd loc thisstat21 = "`r(sestar)'": col`colnum'
		
	test 	posterior + posterior_never_unemp = 0
    estadd loc thisstat23 = string(r(p), "%9.3f"): col`colnum'

	qui sum `choice' if never_unemployed_imp!=.
    estadd loc thisstat25 = r(N): col`colnum'
	
	
	ivreg2 `choice' (posterior posterior_lowcountyunemp = treatment tr_lowcountyunemp) highunemp_rate3y $controls, r
    sigstar posterior, prec(3)
    estadd loc thisstat30 = "`r(bstar)'": col`colnum'
    estadd loc thisstat31 = "`r(sestar)'": col`colnum'
	
    sigstar posterior_lowcountyunemp, prec(3)
    estadd loc thisstat33 = "`r(bstar)'": col`colnum'
    estadd loc thisstat34 = "`r(sestar)'": col`colnum'
		
	test 	posterior + posterior_lowcountyunemp = 0
    estadd loc thisstat36 = string(r(p), "%9.3f"): col`colnum'
	
 	qui sum `choice' if  highunemp_rate3y!=.
    estadd loc thisstat38 = r(N): col`colnum'

    loc ++colnum
    loc colnames "`colnames' `"`: var la `choice''"'"

}

loc rowlabels " " " "{\bf Panel A: Industry}" " " "Posterior: Recession (a)" " " " " "Posterior: Recession $\times$ (b) " "Non-cyclical industry"   " " " Pr(a+b)=0 " " " "Observations" "\hline " "{\bf Panel B: Personal}" "{\bf unemployment history}" " " "Posterior: Recession (a)" " " " " "Posterior: Recession $\times$ (b) " "Never unemployed"   " " " Pr(a+b)=0 " " " "Observations" "\hline" "{\bf Panel C: County}" "{\bf unemployment}" " " "Posterior: Recession (a)" " " " " "Posterior: Recession $\times$ (b) " "Low county unemployment"   " " " Pr(a+b)=0 " " " "Observations" " "  "\hline" " "   "
loc rowstats ""


forval i = 1/38 {
    loc rowstats "`rowstats' thisstat`i'"
}


esttab * using "jobsecurity_het_riskproxies_interact.tex", replace cells(none) booktabs nonotes nomtitles compress alignment(c) nogap noobs nobaselevels label stats(`rowstats', labels(`rowlabels')) ///
      mgroups("\shortstack{Personal\\unemployment}" "\shortstack{Job\\loss}" "\shortstack{Job\\finding}"   , pattern(1 1 1) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))

eststo clear	






























